<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="ARCARC (Automatic Reference Counting) 是由编译器跟运行时共同完成的（运行时标记）；编译器会在编译时会自动加上 retain、release、autorelease、dealloc 操作。 __autoreleasing如果一个变量被用关键字修饰 __autoreleasing 修饰，那么变量会立即加入到自动释放池中 ARC规则 若方法名以下列词语开头，则其返回">
<meta property="og:type" content="article">
<meta property="og:title" content="ARC下，对象什么时候加入自动释放池">
<meta property="og:url" content="https://vhuichen.github.io/2022/03/iOS/iOS%E5%AF%B9%E8%B1%A1%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E5%8A%A0%E5%85%A5%E8%87%AA%E5%8A%A8%E9%87%8A%E6%94%BE%E6%B1%A0/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="ARCARC (Automatic Reference Counting) 是由编译器跟运行时共同完成的（运行时标记）；编译器会在编译时会自动加上 retain、release、autorelease、dealloc 操作。 __autoreleasing如果一个变量被用关键字修饰 __autoreleasing 修饰，那么变量会立即加入到自动释放池中 ARC规则 若方法名以下列词语开头，则其返回">
<meta property="og:locale">
<meta property="article:published_time" content="2022-03-09T16:00:00.000Z">
<meta property="article:modified_time" content="2022-03-10T16:00:00.000Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="自动释放池">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://vhuichen.github.io/2022/03/iOS/iOS%E5%AF%B9%E8%B1%A1%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E5%8A%A0%E5%85%A5%E8%87%AA%E5%8A%A8%E9%87%8A%E6%94%BE%E6%B1%A0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2022/03/iOS/iOS%E5%AF%B9%E8%B1%A1%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E5%8A%A0%E5%85%A5%E8%87%AA%E5%8A%A8%E9%87%8A%E6%94%BE%E6%B1%A0/","path":"2022/03/iOS/iOS对象什么时候加入自动释放池/","title":"ARC下，对象什么时候加入自动释放池"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>ARC下，对象什么时候加入自动释放池 | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#ARC"><span class="nav-number">1.</span> <span class="nav-text">ARC</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#autoreleasing"><span class="nav-number">2.</span> <span class="nav-text">__autoreleasing</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ARC%E8%A7%84%E5%88%99"><span class="nav-number">3.</span> <span class="nav-text">ARC规则</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#objc-autoreleaseReturnValue-%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81%E9%80%BB%E8%BE%91"><span class="nav-number">3.1.</span> <span class="nav-text">objc_autoreleaseReturnValue 相关代码逻辑</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B%E5%AD%90"><span class="nav-number">3.2.</span> <span class="nav-text">例子</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%86%8D%E6%9D%A5%E7%9C%8B%E7%9C%8B%E6%B1%87%E7%BC%96"><span class="nav-number">4.</span> <span class="nav-text">再来看看汇编</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%A4%96%E9%83%A8%E6%96%B9%E6%B3%95%E9%9D%9E%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4%EF%BC%8C%E5%86%85%E9%83%A8%E6%96%B9%E6%B3%95%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4"><span class="nav-number">4.0.1.</span> <span class="nav-text">一、外部方法非关键字开头，内部方法关键字开头</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%A4%96%E9%83%A8%E6%96%B9%E6%B3%95%E9%9D%9E%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4%EF%BC%8C%E5%86%85%E9%83%A8%E6%96%B9%E6%B3%95%E9%9D%9E%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4"><span class="nav-number">4.0.2.</span> <span class="nav-text">二、外部方法非关键字开头，内部方法非关键字开头</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%A4%96%E9%83%A8%E6%96%B9%E6%B3%95%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4%EF%BC%8C%E5%86%85%E9%83%A8%E6%96%B9%E6%B3%95%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4"><span class="nav-number">4.0.3.</span> <span class="nav-text">三、外部方法关键字开头，内部方法关键字开头</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E5%A4%96%E9%83%A8%E6%96%B9%E6%B3%95%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4%EF%BC%8C%E5%86%85%E9%83%A8%E6%96%B9%E6%B3%95%E9%9D%9E%E5%85%B3%E9%94%AE%E5%AD%97%E5%BC%80%E5%A4%B4"><span class="nav-number">4.0.4.</span> <span class="nav-text">四、外部方法关键字开头，内部方法非关键字开头</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95"><span class="nav-number">5.</span> <span class="nav-text">测试</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2022/03/iOS/iOS%E5%AF%B9%E8%B1%A1%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E5%8A%A0%E5%85%A5%E8%87%AA%E5%8A%A8%E9%87%8A%E6%94%BE%E6%B1%A0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="ARC下，对象什么时候加入自动释放池 | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ARC下，对象什么时候加入自动释放池
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2022-03-10 00:00:00" itemprop="dateCreated datePublished" datetime="2022-03-10T00:00:00+08:00">2022-03-10</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2022-03-11 00:00:00" itemprop="dateModified" datetime="2022-03-11T00:00:00+08:00">2022-03-11</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="ARC"><a href="#ARC" class="headerlink" title="ARC"></a>ARC</h3><p>ARC (Automatic Reference Counting) 是由编译器跟运行时共同完成的（运行时标记）；编译器会在编译时会自动加上 retain、release、autorelease、dealloc 操作。</p>
<h3 id="autoreleasing"><a href="#autoreleasing" class="headerlink" title="__autoreleasing"></a>__autoreleasing</h3><p>如果一个变量被用关键字修饰 __autoreleasing 修饰，那么变量会立即加入到自动释放池中</p>
<h3 id="ARC规则"><a href="#ARC规则" class="headerlink" title="ARC规则"></a>ARC规则</h3><ol>
<li><p>若方法名以下列词语开头，则其返回的对象归调用者所有：<br><code>alloc</code>  <code>new</code>  <code>copy</code>  <code>mutableCopy</code>。归调用者所有的意思是：调用上述四种方法的那段代码要负责释放方法所返回的对象。  </p>
</li>
<li><p>除了会自动调用“保留”与“释放”方法外，ARC 还可以执行一些手工操作很难甚至无法完成的优化。如果发现在同一个对象上执行多次“保留”与“释放”操作，那么ARC有时可以成对地移除这两个操作。<br>一般，在方法中返回自动释放的对象时，要执行一个特殊函数。此时不直接调用对象的 autorelease 方法，而是改为调用 objc_autoreleaseReturnValue 。此函数会检视当前方法返回之后即将要执行的那段代码。若发现那段代码在返回的对象上执行 retain 操作，则设置全局数据结构（此数据结构的具体内容因处理器而异）中的一个标志位而不执行 autorelease 操作。与之相似，如果方法返回了一个自动释放的对象，而调用方法的代码要保留此对象，那么此时不直接执行 retain，而是改为执行 objc_retainAutoreleaseReturnValue 函数。此函数要检测刚才提到的那个标志位，若已经置位，则不执行 retain 操作。设置并检测标志位，要比调用 autorelease 和 retain 更快。<br><strong>备注</strong>：objc_autoreleaseReturnValue 优化不一定开启，会根据不同CPU类型决定<br><strong>另外，这个标记位存在哪里呢？关键字：线程局部存储（TLS）</strong></p>
</li>
</ol>
<span id="more"></span>

<h4 id="objc-autoreleaseReturnValue-相关代码逻辑"><a href="#objc-autoreleaseReturnValue-相关代码逻辑" class="headerlink" title="objc_autoreleaseReturnValue 相关代码逻辑"></a>objc_autoreleaseReturnValue 相关代码逻辑</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// Prepare a value at +1 for return through a +0 autoreleasing convention.</span><br><span class="line">id objc_autoreleaseReturnValue(id obj) &#123;</span><br><span class="line">    if (prepareOptimizedReturn(ReturnAtPlus1)) return obj;</span><br><span class="line">    return objc_autorelease(obj);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">static ALWAYS_INLINE bool prepareOptimizedReturn(ReturnDisposition disposition) &#123;</span><br><span class="line">    ASSERT(getReturnDisposition() == ReturnAtPlus0);</span><br><span class="line">    //callerAcceptsOptimizedReturn 是个条件编译选项，不同CPU类型代码完全不一样</span><br><span class="line">    if (callerAcceptsOptimizedReturn(__builtin_return_address(0))) &#123;</span><br><span class="line">        if (disposition) setReturnDisposition(disposition);</span><br><span class="line">        return true;</span><br><span class="line">    &#125;</span><br><span class="line">    return false;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">// 方法名以关键字 new 开头，ARC 不会加入 retain、release 或 autorelease 语句。</span><br><span class="line">+ (VCHPerson *)newPerson &#123;</span><br><span class="line">    VCHPerson *person = [[VCHPerson alloc] init];</span><br><span class="line">    return person;</span><br><span class="line">&#125;</span><br><span class="line">// 方法名不以关键字开头，ARC 会自动加上 autorelease 语句。</span><br><span class="line">+ (VCHPerson *)somePerson &#123;</span><br><span class="line">    VCHPerson *person = [[VCHPerson alloc] init];</span><br><span class="line">    return person;</span><br><span class="line">&#125;</span><br><span class="line">// ARC 会在函数末尾给 personOne 加上 release 语句。</span><br><span class="line">// 而 somePerson 已经在方法内部加入到释放池中了</span><br><span class="line">- (void)doSomething &#123;</span><br><span class="line">    VCHPerson *personOne = [VCHPerson newPerson];</span><br><span class="line">    VCHPerson *personTwo = [VCHPerson somePerson];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>内部的实现逻辑可以用以下代码代替</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//非关键字开头</span><br><span class="line">+ (instancetype)object &#123;</span><br><span class="line">    return [[NSObject alloc] init];</span><br><span class="line">    // 实际会变成</span><br><span class="line">    return objc_autoreleaseReturnValue([[NSObject alloc] init]);</span><br><span class="line">&#125;</span><br><span class="line">NSObject *object = [NSObject object];</span><br><span class="line">// 实际会变成</span><br><span class="line">NSObject *object = objc_retainAutoreleasedReturnValue([NSObject object]);</span><br></pre></td></tr></table></figure>

<h3 id="再来看看汇编"><a href="#再来看看汇编" class="headerlink" title="再来看看汇编"></a>再来看看汇编</h3><h5 id="一、外部方法非关键字开头，内部方法关键字开头"><a href="#一、外部方法非关键字开头，内部方法关键字开头" class="headerlink" title="一、外部方法非关键字开头，内部方法关键字开头"></a>一、外部方法非关键字开头，内部方法关键字开头</h5><ol>
<li>NSMutableArray 以 alloc 方式生成对象，引用计数为1，此时没有加入到自动释放池中</li>
<li>init方法不做任何处理，猜测是编译器行为</li>
<li>当前方法名不以关键字开头，return时需要加入到自动释放池中，表示当前方法内持有对象，并负责释放对象。</li>
</ol>
<p>但是由于编译器优化，不会立即加入到自动释放池中，而是调用 objc_autoreleaseReturnValue 方法标记对象（从下面的汇编代码可以看出确实调用了 objc_autoreleaseReturnValue 方法）。<br>如果对象返回后，又有别的变量需要 retain 这个对象，则编译器会调用这个方法 objc_retainAutoreleasedReturnValue，此时会检查对象的是否已经被标记，如果已经被标记，则相互抵消，并将标记位清除，如果没有被标记则最终调用 retain 方法。  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">- (id)getObjectWithAlloc &#123;</span><br><span class="line">    id obj = [[NSMutableArray alloc] init];</span><br><span class="line">    return obj;//断点</span><br><span class="line">    /*</span><br><span class="line">     VCHTest`-[AppDelegate getObjectWithAlloc]:</span><br><span class="line">         0x10027e210 &lt;+0&gt;:  pushq  %rbp</span><br><span class="line">         0x10027e211 &lt;+1&gt;:  movq   %rsp, %rbp</span><br><span class="line">         0x10027e214 &lt;+4&gt;:  subq   $0x20, %rsp</span><br><span class="line">         0x10027e218 &lt;+8&gt;:  movq   %rdi, -0x8(%rbp)</span><br><span class="line">         0x10027e21c &lt;+12&gt;: movq   %rsi, -0x10(%rbp)</span><br><span class="line">         0x10027e220 &lt;+16&gt;: movq   0x9c41(%rip), %rdi        ; (void *)0x00007fff801e6298: NSMutableArray</span><br><span class="line">         0x10027e227 &lt;+23&gt;: callq  0x10027f814               ; symbol stub for: objc_alloc_init</span><br><span class="line">         0x10027e22c &lt;+28&gt;: movq   %rax, -0x18(%rbp)</span><br><span class="line">     -&gt;  0x10027e230 &lt;+32&gt;: movq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e234 &lt;+36&gt;: movq   0x3df5(%rip), %rax        ; (void *)0x00007fff20191840: objc_retain</span><br><span class="line">         0x10027e23b &lt;+43&gt;: callq  *%rax</span><br><span class="line">         0x10027e23d &lt;+45&gt;: xorl   %ecx, %ecx</span><br><span class="line">         0x10027e23f &lt;+47&gt;: movl   %ecx, %esi</span><br><span class="line">         0x10027e241 &lt;+49&gt;: leaq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e245 &lt;+53&gt;: movq   %rax, -0x20(%rbp)</span><br><span class="line">         0x10027e249 &lt;+57&gt;: callq  0x10027f86e               ; symbol stub for: objc_storeStrong</span><br><span class="line">         0x10027e24e &lt;+62&gt;: movq   -0x20(%rbp), %rdi</span><br><span class="line">         0x10027e252 &lt;+66&gt;: addq   $0x20, %rsp</span><br><span class="line">         0x10027e256 &lt;+70&gt;: popq   %rbp</span><br><span class="line">         0x10027e257 &lt;+71&gt;: jmp    0x10027f826               ; symbol stub for: objc_autoreleaseReturnValue</span><br><span class="line">     */</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="二、外部方法非关键字开头，内部方法非关键字开头"><a href="#二、外部方法非关键字开头，内部方法非关键字开头" class="headerlink" title="二、外部方法非关键字开头，内部方法非关键字开头"></a>二、外部方法非关键字开头，内部方法非关键字开头</h5><ol>
<li>NSMutableArray 以 array 方式生成对象，引用计数为1，并加入到自动释放池中（标记）</li>
<li>调用 objc_retainAutoreleasedReturnValue，因为临时变量需要持有对象，此时标记位清除</li>
<li>由于方法名不以关键字开头，return 时调用 objc_autoreleaseReturnValue 加入到自动释放池中（同样也是先标记）</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (id)getObjectWithArray &#123;</span><br><span class="line">    id obj = [NSMutableArray array];</span><br><span class="line">    return obj;//断点</span><br><span class="line">    /*</span><br><span class="line">     VCHTest`-[AppDelegate getObjectWithArray]:</span><br><span class="line">         0x10027e260 &lt;+0&gt;:  pushq  %rbp</span><br><span class="line">         0x10027e261 &lt;+1&gt;:  movq   %rsp, %rbp</span><br><span class="line">         0x10027e264 &lt;+4&gt;:  subq   $0x20, %rsp</span><br><span class="line">         0x10027e268 &lt;+8&gt;:  movq   %rdi, -0x8(%rbp)</span><br><span class="line">         0x10027e26c &lt;+12&gt;: movq   %rsi, -0x10(%rbp)</span><br><span class="line">         0x10027e270 &lt;+16&gt;: movq   0x9bf1(%rip), %rdi        ; (void *)0x00007fff801e6298: NSMutableArray</span><br><span class="line">         0x10027e277 &lt;+23&gt;: movq   0x9aba(%rip), %rsi        ; &quot;array&quot;</span><br><span class="line">         0x10027e27e &lt;+30&gt;: movq   0x3d9b(%rip), %rax        ; (void *)0x00007fff20175280: objc_msgSend</span><br><span class="line">         0x10027e285 &lt;+37&gt;: callq  *%rax</span><br><span class="line">         0x10027e287 &lt;+39&gt;: movq   %rax, %rdi</span><br><span class="line">         0x10027e28a &lt;+42&gt;: callq  0x10027f85c               ; symbol stub for: objc_retainAutoreleasedReturnValue</span><br><span class="line">         0x10027e28f &lt;+47&gt;: movq   %rax, -0x18(%rbp)</span><br><span class="line">     -&gt;  0x10027e293 &lt;+51&gt;: movq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e297 &lt;+55&gt;: movq   0x3d92(%rip), %rax        ; (void *)0x00007fff20191840: objc_retain</span><br><span class="line">         0x10027e29e &lt;+62&gt;: callq  *%rax</span><br><span class="line">         0x10027e2a0 &lt;+64&gt;: xorl   %ecx, %ecx</span><br><span class="line">         0x10027e2a2 &lt;+66&gt;: movl   %ecx, %esi</span><br><span class="line">         0x10027e2a4 &lt;+68&gt;: leaq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e2a8 &lt;+72&gt;: movq   %rax, -0x20(%rbp)</span><br><span class="line">         0x10027e2ac &lt;+76&gt;: callq  0x10027f86e               ; symbol stub for: objc_storeStrong</span><br><span class="line">         0x10027e2b1 &lt;+81&gt;: movq   -0x20(%rbp), %rdi</span><br><span class="line">         0x10027e2b5 &lt;+85&gt;: addq   $0x20, %rsp</span><br><span class="line">         0x10027e2b9 &lt;+89&gt;: popq   %rbp</span><br><span class="line">         0x10027e2ba &lt;+90&gt;: jmp    0x10027f826               ; symbol stub for: objc_autoreleaseReturnValue</span><br><span class="line">     */</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="三、外部方法关键字开头，内部方法关键字开头"><a href="#三、外部方法关键字开头，内部方法关键字开头" class="headerlink" title="三、外部方法关键字开头，内部方法关键字开头"></a>三、外部方法关键字开头，内部方法关键字开头</h5><ol>
<li>NSMutableArray 使用 alloc 生成对象，引用计数为1，内部没有加入 AutoreleasePool 中；</li>
<li>随后调用 init 方法，这里 init 方法返回对象时没有加入到自动释放池中，猜测是编译器行为；</li>
<li>由于当前方法以 copy 开头，return 时既不调用 release，也不调用 autorelease；</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">- (id)copyObjectWithAlloc &#123;</span><br><span class="line">    id obj = [[NSMutableArray alloc] init];</span><br><span class="line">    return obj;//断点</span><br><span class="line">    /*</span><br><span class="line">     VCHTest`-[AppDelegate copyObjectWithAlloc]:</span><br><span class="line">         0x10027e160 &lt;+0&gt;:  pushq  %rbp</span><br><span class="line">         0x10027e161 &lt;+1&gt;:  movq   %rsp, %rbp</span><br><span class="line">         0x10027e164 &lt;+4&gt;:  subq   $0x20, %rsp</span><br><span class="line">         0x10027e168 &lt;+8&gt;:  movq   %rdi, -0x8(%rbp)</span><br><span class="line">         0x10027e16c &lt;+12&gt;: movq   %rsi, -0x10(%rbp)</span><br><span class="line">         0x10027e170 &lt;+16&gt;: movq   0x9cf1(%rip), %rax        ; (void *)0x00007fff801e6298: NSMutableArray</span><br><span class="line">         0x10027e177 &lt;+23&gt;: movq   %rax, %rdi</span><br><span class="line">         0x10027e17a &lt;+26&gt;: callq  0x10027f814               ; symbol stub for: objc_alloc_init</span><br><span class="line">         0x10027e17f &lt;+31&gt;: movq   %rax, -0x18(%rbp)</span><br><span class="line">     -&gt;  0x10027e183 &lt;+35&gt;: movq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e187 &lt;+39&gt;: callq  *0x3ea3(%rip)             ; (void *)0x00007fff20191840: objc_retain</span><br><span class="line">         0x10027e18d &lt;+45&gt;: xorl   %ecx, %ecx</span><br><span class="line">         0x10027e18f &lt;+47&gt;: movl   %ecx, %esi</span><br><span class="line">         0x10027e191 &lt;+49&gt;: leaq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e195 &lt;+53&gt;: movq   %rax, -0x20(%rbp)</span><br><span class="line">         0x10027e199 &lt;+57&gt;: callq  0x10027f86e               ; symbol stub for: objc_storeStrong</span><br><span class="line">         0x10027e19e &lt;+62&gt;: movq   -0x20(%rbp), %rax</span><br><span class="line">         0x10027e1a2 &lt;+66&gt;: addq   $0x20, %rsp</span><br><span class="line">         0x10027e1a6 &lt;+70&gt;: popq   %rbp</span><br><span class="line">         0x10027e1a7 &lt;+71&gt;: retq</span><br><span class="line">     */</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="四、外部方法关键字开头，内部方法非关键字开头"><a href="#四、外部方法关键字开头，内部方法非关键字开头" class="headerlink" title="四、外部方法关键字开头，内部方法非关键字开头"></a>四、外部方法关键字开头，内部方法非关键字开头</h5><ol>
<li>NSMutableArray 使用 array 生成对象，引用计数为1，内部已经加入 AutoreleasePool 中（其实还没有加入，只是被标记了而已）；</li>
<li>通过下面的汇编代码可以看出，方法内调用了 objc_retainAutoreleasedReturnValue （因为有个临时变量需要持有当前对象），此时发现该对象已经被标记，则相互抵消，清除标志位，不加入自动释放池中；</li>
<li>由于当前方法以 copy 开头，return 时既不调用 release，也不调用 autorelease；</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">- (id)copyObjectWithArray &#123;</span><br><span class="line">    id obj = [NSMutableArray array];</span><br><span class="line">    return obj;//断点</span><br><span class="line">    /*</span><br><span class="line">     VCHTest`-[AppDelegate copyObjectWithArray]:</span><br><span class="line">         0x10027e1b0 &lt;+0&gt;:  pushq  %rbp</span><br><span class="line">         0x10027e1b1 &lt;+1&gt;:  movq   %rsp, %rbp</span><br><span class="line">         0x10027e1b4 &lt;+4&gt;:  subq   $0x20, %rsp</span><br><span class="line">         0x10027e1b8 &lt;+8&gt;:  movq   %rdi, -0x8(%rbp)</span><br><span class="line">         0x10027e1bc &lt;+12&gt;: movq   %rsi, -0x10(%rbp)</span><br><span class="line">         0x10027e1c0 &lt;+16&gt;: movq   0x9ca1(%rip), %rax        ; (void *)0x00007fff801e6298: NSMutableArray</span><br><span class="line">         0x10027e1c7 &lt;+23&gt;: movq   0x9b6a(%rip), %rsi        ; &quot;array&quot;</span><br><span class="line">         0x10027e1ce &lt;+30&gt;: movq   %rax, %rdi</span><br><span class="line">         0x10027e1d1 &lt;+33&gt;: callq  *0x3e49(%rip)             ; (void *)0x00007fff20175280: objc_msgSend</span><br><span class="line">         0x10027e1d7 &lt;+39&gt;: movq   %rax, %rdi</span><br><span class="line">         0x10027e1da &lt;+42&gt;: callq  0x10027f85c               ; symbol stub for: objc_retainAutoreleasedReturnValue</span><br><span class="line">         0x10027e1df &lt;+47&gt;: movq   %rax, -0x18(%rbp)</span><br><span class="line">     -&gt;  0x10027e1e3 &lt;+51&gt;: movq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e1e7 &lt;+55&gt;: callq  *0x3e43(%rip)             ; (void *)0x00007fff20191840: objc_retain</span><br><span class="line">         0x10027e1ed &lt;+61&gt;: xorl   %ecx, %ecx</span><br><span class="line">         0x10027e1ef &lt;+63&gt;: movl   %ecx, %esi</span><br><span class="line">         0x10027e1f1 &lt;+65&gt;: leaq   -0x18(%rbp), %rdi</span><br><span class="line">         0x10027e1f5 &lt;+69&gt;: movq   %rax, -0x20(%rbp)</span><br><span class="line">         0x10027e1f9 &lt;+73&gt;: callq  0x10027f86e               ; symbol stub for: objc_storeStrong</span><br><span class="line">         0x10027e1fe &lt;+78&gt;: movq   -0x20(%rbp), %rax</span><br><span class="line">         0x10027e202 &lt;+82&gt;: addq   $0x20, %rsp</span><br><span class="line">         0x10027e206 &lt;+86&gt;: popq   %rbp</span><br><span class="line">         0x10027e207 &lt;+87&gt;: retq</span><br><span class="line">     */</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><p>下面代码执行会怎样</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">id __unsafe_unretained obj0 = nil;</span><br><span class="line">id __unsafe_unretained obj1 = nil;</span><br><span class="line">&#123;</span><br><span class="line">    NSArray *array0 = [self getObjectWithAlloc];</span><br><span class="line">    obj0 = array0;</span><br><span class="line">    //</span><br><span class="line">    NSArray *array1 = [self copyObjectWithAlloc];</span><br><span class="line">    obj1 = array1;</span><br><span class="line">&#125;</span><br><span class="line">NSLog(@&quot;vhuichen obj0 = %@&quot;, obj0);</span><br><span class="line">NSLog(@&quot;vhuichen obj1 = %@&quot;, obj1);</span><br></pre></td></tr></table></figure>







    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%87%AA%E5%8A%A8%E9%87%8A%E6%94%BE%E6%B1%A0/" rel="tag"># 自动释放池</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/01/NetworkProtocol/WebSocket/WebSocket/" rel="prev" title="WebSocket">
                  <i class="fa fa-chevron-left"></i> WebSocket
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/04/iOS/libffi%E6%8E%A2%E7%A9%B6%20/" rel="next" title="libffi 探究">
                  libffi 探究 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
